
#include "appXssl.h"

static int console_putc (int ch)
{
  if (ch == '\n')  {
    while (!(USART1->SR & 0x0080));
    USART1->DR = 0x0D;
  }
  if (ch == '\r')  {
    while (!(USART1->SR & 0x0080));
    USART1->DR = 0x0A;
  }
  while (!(USART1->SR & 0x0080));
  USART1->DR = (ch & 0xFF);
  return (ch);
}

void my_printf(const char *format, ...)
{
	char cFLen = 0,
			 cWidth = 0,
			 cCh = 0;
	va_list arg;
	va_start (arg, format);

	while( *format != '\0')
	{
		if( *format == '%')
		{ 
			cWidth = 0;
			cCh = 0;
			cFLen += 1;
			if( *(format+cFLen) == '0' )
			{
				cFLen += 1;
				cCh = *(format+cFLen);
				if(*(format+cFLen) > '0' && *(format+cFLen) <= '9')
				{
					cWidth = *(format+cFLen) - '0';
					cFLen += 1;
				}
			}else if(*(format+cFLen) > '0' && *(format+cFLen) <= '9')
			{
				cCh = ' ';
				cWidth = *(format+cFLen) - '0';
				cFLen += 1;
			}
			if( *(format+cFLen) == 'c' )
			{
				char c = (char)va_arg(arg, int);
				console_putc(c);
			} else if( *(format+cFLen) == 'd')
			{
				char store[20] = {0};
				int i = va_arg(arg, int);
				char* str = store;
				sprintf(str,"%d", i);
				if(cCh)
					while(strlen(str) < cWidth) {console_putc(cCh); cWidth--;}
				while( *str != '\0') console_putc(*str++); 
			} else if( *(format+cFLen) == 'x' || *(format+cFLen) == 'X')
			{
				char store[20]= {0};
				int i = va_arg(arg, int);
				char* str = store;
				sprintf(str,"%02X", i);
				if(cCh)
					while(strlen(str) < cWidth) {console_putc(cCh); cWidth--;}
				while( *str != '\0') console_putc(*str++); 
			} else if( *(format+cFLen) == 's' )
			{
				char* str = va_arg(arg, char*);
				while( *str != '\0') console_putc(*str++);
			}
			cFLen += 1;
			format += cFLen;
			cFLen = 0;
		} else {
			console_putc(*format++);
		}
	}
	va_end (arg);
}

void console_log(const char *format, ...)
{
  char cFLen = 0,
       cWidth = 0,
       cCh = 0;
	int iCh = 0;
  va_list arg;
  va_start (arg, format);

  while( *format != '\0'){
    if( *format == '%'){ 
      cWidth = 0;
      cCh = 0;
      cFLen += 1;
      if( *(format+cFLen) == '0' ){
        cFLen += 1;
        cCh = *(format+cFLen);
        if(*(format+cFLen) > '0' && *(format+cFLen) <= '9'){
          cWidth = *(format+cFLen) - '0';
          cFLen += 1;
        }
      }else if(*(format+cFLen) > '0' && *(format+cFLen) <= '9'){
        cCh = ' ';
        cWidth = *(format+cFLen) - '0';
        cFLen += 1;
      }
      if( *(format+cFLen) == 'c' ){
        char c = (char)va_arg(arg, int);
        console_putc(c);
      } else if( *(format+cFLen) == 'd'){
        char store[20] = {0};
        int i = va_arg(arg, int);
        char* str = store;
        sprintf(str,"%d", i);
        if(cCh)
          while(strlen(str) < cWidth) {console_putc(cCh); cWidth--;}
        while( *str != '\0') console_putc(*str++); 
      } else if( *(format+cFLen) == 'x' || *(format+cFLen) == 'X'){
        char store[20]= {0};
        int i = va_arg(arg, int);
        char* str = store;
        sprintf(str,"%02X", i);
        if(cCh)
          while(strlen(str) < cWidth) {console_putc(cCh); cWidth--;}
        while( *str != '\0') console_putc(*str++); 
      } else if( *(format+cFLen) == 's' ){
        char* str = va_arg(arg, char*);
        while( *str != '\0') console_putc(*str++);
      }
      cFLen += 1;
      format += cFLen;
      cFLen = 0;
    } else {
    	iCh = *format++;
			/*
			if (iCh == '\n'){
				iCh = 0x0D;
		  }
		  if (iCh == '\r'){
				iCh = 0x0A;
		  }
			uart1_send_char(iCh);
      */
      console_putc(iCh);  
    }
  }
  va_end (arg);
}

static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static signed char index_64[128] = {	
	    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,	
	    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,	
	    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,	
	    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,	
	    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,	
	    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,	
	    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,	
	    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1	
} ;

#define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])

unsigned int com_base64_encode(const unsigned char *pucSrcData, unsigned int uiSrcLen, char *pcDestBuf) 
{
	unsigned char oval = 0 ; 	
  char *result = pcDestBuf;	
  char *out = result;	
	unsigned int uiLen = 0;
	
	while (uiSrcLen >= 3) {	
		*out++ = basis_64[pucSrcData[0] >> 2];	
		*out++ = basis_64[((pucSrcData[0] << 4) & 0x30) | (pucSrcData[1] >> 4)];	
		*out++ = basis_64[((pucSrcData[1] << 2) & 0x3C) | (pucSrcData[2] >> 6)];	
		*out++ = basis_64[pucSrcData[2] & 0x3F];	
		pucSrcData += 3;	
		uiSrcLen -= 3;	
	}
	if (uiSrcLen > 0) {	
		*out++ = basis_64[pucSrcData[0] >> 2];	
		oval = (pucSrcData[0] << 4) & 0x30 ;	
		if (uiSrcLen > 1) {
			oval |= pucSrcData[1] >> 4;
		}
		*out++ = basis_64[oval];	
		*out++ = (uiSrcLen < 2) ? '=' : basis_64[(pucSrcData[1] << 2) & 0x3C];	
		*out++ = '=';	
	}	
	*out = '\0';		
	uiLen = strlen(pcDestBuf);	
	return uiLen;
}	

unsigned int com_base64_decode(const char *pcSrcData, unsigned char *pucDestBuf)	
{		
	int c1, c2, c3, c4;	 
	unsigned char *result = pucDestBuf;	
	unsigned char *out = result;	
	unsigned int uiLen = 0;
		
	while (1) {	
		if (pcSrcData[0]==0) {
			*out = '\0' ; 
			return uiLen;
		}
		c1 = pcSrcData[0];	
		if (CHAR64(c1) == -1) {goto base64_decode_error;}
		
		c2 = pcSrcData[1];	
		if (CHAR64(c2) == -1) {goto base64_decode_error;}
		
		c3 = pcSrcData[2];	
		if ((c3 != '=') && (CHAR64(c3) == -1)) {goto base64_decode_error;}
		
		c4 = pcSrcData[3];	
		if ((c4 != '=') && (CHAR64(c4) == -1)) {goto base64_decode_error;}
		
		pcSrcData += 4;	
		*out++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4);	
		uiLen += 1;	
		if (c3 != '=') {	
			*out++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2);	
			uiLen += 1;	
			if (c4 != '=') {	
				*out++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4);	
				uiLen += 1;	
			}
		}	
	}	
	base64_decode_error:	
	uiLen = 0;	
	return uiLen;
}

void print_hex(char *pcTag, const unsigned char *pucData, unsigned int uiLen)
{
  unsigned int i;
  print_log(D_DEBUG,("%s[%d]:\n", pcTag, uiLen));
  for(i=0; i<uiLen; i++){
    print_log(D_DEBUG,("0x%X ", pucData[i]));
  }
  print_log(D_DEBUG,("\n"));
}

void delayus(unsigned int nCount)
{
	for(; nCount != 0; nCount--){
    ;
	}
}

#if defined(NET)
/* no ethernet, jus define for TCP_CM3.lib */
void init_ethernet (void)
{
	/* TODO */
}

void send_frame (OS_FRAME *frame) 
{
	/* TODO*/
}

char *dbg_time (void)
{
	return 0;
}
char *dbg_proc (U32 proc)
{
	return 0;
}
#endif

